package com.lc.demo.controller;

import com.lc.demo.bean.Address;
import com.lc.demo.bean.User;
import com.lc.demo.result.ResponseMessage;
import com.lc.demo.service.AddressService;
import com.lc.demo.service.UserService;
import com.lc.demo.util.JwtUtil;
import io.jsonwebtoken.Claims;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author: sakila
 * @Date: 2022/03/07/18:51
 * @Description:
 */

@CrossOrigin
@RestController
@RequestMapping("/api/address")
public class AddressController {


    @Autowired
    private AddressService addressService;

    @Autowired
    private UserService userService;

    //获取地址列表
    @GetMapping()
    public ResponseMessage getAddressList(HttpServletRequest request) {

        String authorization = request.getHeader("Authorization");
        // 5. 请求头中获取令牌
        Claims claims = JwtUtil.parseJWT(authorization);
        String subject = claims.getSubject();
        User userFind = new User();
        userFind.setEmail(subject);
        User one = userService.findOne(userFind);
        Integer userId = one.getId();

        List<Address> addressList = addressService.selectAddressList(userId);
        return ResponseMessage.successWithData(addressList);
    }

    //添加地址
    @PostMapping()
    public ResponseMessage addressAdd(@RequestBody Address address, HttpServletRequest request) {

        String authorization = request.getHeader("Authorization");
        // 5. 请求头中获取令牌
        Claims claims = JwtUtil.parseJWT(authorization);
        String subject = claims.getSubject();
        User userFind = new User();
        userFind.setEmail(subject);
        User one = userService.findOne(userFind);
        Integer userId = one.getId();
        address.setUserId(userId);
        addressService.addAddress(address);
        return ResponseMessage.success();
    }

    //addressDetail
    //根据Id查询地址详情
    @GetMapping("/{id}")
    public ResponseMessage addressDetail(@PathVariable("id") Integer id, HttpServletRequest request) {

        String authorization = request.getHeader("Authorization");
         Address address=  addressService.findById(id);
        return ResponseMessage.successWithData(address);
    }

    //addressDetail
    //修改地址
    @PutMapping("/{id}")
    public ResponseMessage addressEdit(@PathVariable("id") Integer id, @RequestBody Address address) {

        address.setId(id);
        addressService.updateAddress(address);

        return ResponseMessage.success();
    }

    //设置默认地址
    @GetMapping("/{id}/default")
    public ResponseMessage addressDefault(@PathVariable("id") Integer id,HttpServletRequest request) {
        String authorization = request.getHeader("Authorization");
        // 5. 请求头中获取令牌
        Claims claims = JwtUtil.parseJWT(authorization);
        String subject = claims.getSubject();
        User userFind = new User();
        userFind.setEmail(subject);
        User one = userService.findOne(userFind);
        Integer userId = one.getId();
        addressService.addressDefault(id,userId);

        return ResponseMessage.success();
    }

    //删除地址
    @Delete("/{id}")
    public ResponseMessage delete(@PathVariable("id") Integer id) {

        addressService.delete(id);

        return ResponseMessage.success();
    }

    //String authorization = request.getHeader("Authorization");
    //        // 5. 请求头中获取令牌
    //        Claims claims = JwtUtil.parseJWT(authorization);
    //        String subject = claims.getSubject();
    //        User userFind=new User();
    //        userFind.setEmail(subject);
    //        User one = userService.findOne(userFind);


}
